package com.hanbang.taitaigou.ui.activity.application;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;

import com.ant.liao.GifView;
import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.ui.activity.base.BaseActivity;
import com.hanbang.taitaigou.ui.activity.home.MainActivity;
import com.hanbang.taitaigou.utils.other.ObjectUtils;
import com.hanbang.taitaigou.utils.other.SharedPreferencesUtils;

import java.util.Timer;
import java.util.TimerTask;

public class WelcomeActivity extends BaseActivity {
    private int time = 3000;
    private Timer timer = new Timer();
    private boolean isToNext = false;
    TimerTask timerTask;
    private GifView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = new GifView(this);
//        view.setGifImageType(GifImageType.);
        // 设置Gif图片源
        view.setGifImage(R.mipmap.welcom);
        // 添加监听器
//         gf1.setOnClickListener((View.OnClickListener) this);
        // 设置显示的大小，拉伸或者压缩
        DisplayMetrics dm = new DisplayMetrics();
        //获取屏幕信息
                getWindowManager().getDefaultDisplay().getMetrics(dm);
                int screenWidth = dm.widthPixels;
               int screenHeigh = dm.heightPixels;
        view.setShowDimension(screenWidth, screenHeigh);
// 设置加载方式：先加载后显示、边加载边显示、只显示第一帧再显示
        view.setGifImageType(GifView.GifImageType.COVER);
        setContentView(view);
        timer.schedule(timerTask = new TimerTask() {
            @Override
            public void run() {
                while (!isToNext) {
                }
                myHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        timer.cancel();
                        Intent intent = new Intent();
                        intent.setClass(WelcomeActivity.this, MainActivity.class);
//                        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                        startActivity(intent);
                        finish();
                    }
                });
            }
        }, time);
        ((MyApplication) getApplication()).getZuijin();
        //next();
        getDbUserData();
        checkIsFirst();
        isToNext = true;
    }

    private void checkIsFirst() {
        if (SharedPreferencesUtils.getSharedPreferencesKeyAndValue(this, "Run",
                "VersionCode", -1) != ObjectUtils.getVersionCode(this)) {
            // 第一次
            SharedPreferencesUtils.setSharedPreferencesKeyAndValue(this, "Run",
                    "VersionCode", ObjectUtils.getVersionCode(this));
            Intent intent = new Intent(this, LeadActivity.class);
            startActivity(intent);
            timerTask.cancel();
            timer.cancel();
            finish();
        }
    }

    @SuppressLint("HandlerLeak")
    Handler myHandler = new Handler();

    private void next() {
        if (isLogin(false)) {
            // 检查密码的正确性

        } else {
            // 未登录
            isToNext = true;
        }

    }

}
